<template>
  <div class="test-map">
    <div class="header_body">
      <header-nav :leftArrow="true" :rightArrow="true" titelText="坐标设置" @goBack="goBack" @submit="submit"></header-nav>
    </div>

    <AMapComponents ref="locationMap" :is-modify="true" :center="center" :defalutModifyList="defalutModifyList"
      @mapClickOK="mapClickOK" />
  </div>
</template>
<script>
const { useVision } = $globalStore
import AMapComponents from '@/components/AMapComponents'
import {
  showNotify,
} from 'vant'
export default {
  name: 'TestMap',
  components: {
    AMapComponents
  },
  computed: {
    defalutModifyList() {
      return this.$route.query.defaultLocation && JSON.parse(this.$route.query.defaultLocation)
    },
    // 中心点
    center() {
      return (this.$route.query.type === 'location' && this.defalutModifyList) ? this.defalutModifyList[0].lnglat : []
    }
  },
  mounted() {

  },
  methods: {
    mapClickOK(data, i, e) {
      console.log(data, i, e, 'mapClickOK');
    },
    submit() {
      if (this.$refs.locationMap.markerList[0]) {
        const locationObj = this.$refs.locationMap.markerList[0].regeocode
        if (this.$route.query.from) {
          if (this.$route.query.mapUpdFlag != 1) {
            showNotify({ type: 'danger', message: '当前账号无法修改经纬度' })
          } else {
            const lnglat = locationObj.position.lng + ',' + locationObj.position.lat
            let params = {
              id: this.$route.query.placeId,
              longitude: locationObj.position.lng,
              latitude: locationObj.position.lat,
            }
            useVision.updMapMsg(params).then(res => {
              this.$router.push({
                path: this.$route.query.path,
                query: { id: this.$route.query.id, from: this.$route.query.from, lnglat }
              })
            })
          }
        } else {
          const taskForm = JSON.parse(this.$route.query.taskForm)
          taskForm.placeInfo.lnglat = locationObj.position.lng + ',' + locationObj.position.lat
          this.$router.push({
            path: this.$route.query.path,
            query: { id: this.$route.query.id, taskForm: JSON.stringify(taskForm) }
          })
        }
      } else {
        showNotify({ type: 'danger', message: '请选择/修改地址' })
      }
    }
  }
}
</script>

<style scoped lang="scss">
.test-map {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
}
</style>
